package project06.pmath;                       
import static java.lang.Math.sqrt;

public class VectorMath{
	
	private double []vec;
	private int n;
	
	public VectorMath(int n){
		this.vec = new double[n];
		this.n = n;
	}

	public int getDim() {
		return n;
	}
	
	public double getValue(int index){
		
		if(vec != null)
			if (n != 0 && index < n && index >= 0)
				return vec[index];
		
		return 0;
	}
	
	public void setValue(int index, double value){
		
		if(vec != null)
			if(n != 0 && index < n && index >= 0)
				vec[index] = value;
	}
	
	public String toString(){
		String aux = "(";
		
		if(n == 0)
			return "(0)";
		
		for(int i = 0; i < n; i++) {
			aux += this.vec[i];
			
			if(i != n - 1)
				aux += ", ";
			else
				aux += ") ";
		}
		
		return aux;
	}
	
	public VectorMath clone(){
		VectorMath clon = new VectorMath(this.n);
		
		for(int i = 0; i < this.n; i++)
			clon.vec[i] = this.vec[i];
		
		return clon;
	}
	
	public boolean equals(VectorMath vm){
		
		if(vm == null)
			return false;
		
		if(vm.n != this.n)
			return false;
		
		for(int i = 0; i < n; i++)
			if(this.vec[i] != vm.vec[i])
				return false;
		
		return true;
	}
	
	public VectorMath suma(VectorMath v){
		VectorMath aux = this.clone(); 
		
		if(v.n == this.n)
			for(int i = 0; i < n; i++)
				aux.vec[i] +=  v.vec[i];
		
		return aux;
	}
	
	public VectorMath productoEscalar(double k){
		VectorMath aux = new VectorMath(this.n);
		
		for(int i = 0; i < n; i++)
			aux.vec[i] = this.vec[i] * k;
		
		return aux;
	}
	
	public double productoVector(VectorMath v){
		
		double aux = 0;
		
		if(v.n == this.n)
			for(int i = 0; i < n; i++)
				aux += this.vec[i] + v.vec[i];
		
		return aux;
	}

	
	public double normaUno(){
		double x = 0;
		
		for(int i = 0; i < n; i++)
			x += vec[i];
		
		return x;
	}
	
	public double normaDos(){
		double x = 0;
		
		for(int i = 0; i < n; i++)
			x += vec[i] * vec[i];
		
		return sqrt(x);
	}
	
	public double normaInfinito(){
		double x_max = vec[0];
		
		for(int i = 0; i < n ; i++){
			if(vec[i] > x_max)
				x_max = vec[i];

		}
					
		return x_max;
	}
	

}
